//
//  CustomButton.m
//  OCStarterProject
//
//  Created by huaya on 2025/10/2.
//

#import "CustomButton.h"

@interface CustomButton ()

@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, strong) NSString *originalTitle;
@property (nonatomic, strong) UIImage *originalImage;

@end

@implementation CustomButton

- (instancetype)initWithFrame:(CGRect)frame style:(CustomButtonStyle)style {
    self = [super initWithFrame:frame];
    if (self) {
        _buttonStyle = style;
        [self setupButton];
        [self setupLoadingIndicator];
    }
    return self;
}

- (void)setupButton {
    // 设置基本属性
    self.layer.cornerRadius = 8.0;
    self.layer.masksToBounds = YES;
    self.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    
    // 根据样式设置外观
    [self updateButtonAppearance];
    
    // 添加点击事件
    [self addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupLoadingIndicator {
    _loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    _loadingIndicator.hidesWhenStopped = YES;
    _loadingIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_loadingIndicator];
    
    [NSLayoutConstraint activateConstraints:@[
        [_loadingIndicator.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        [_loadingIndicator.centerYAnchor constraintEqualToAnchor:self.centerYAnchor]
    ]];
}

- (void)updateButtonAppearance {
    switch (self.buttonStyle) {
        case CustomButtonStylePrimary:
            [self setBackgroundColor:[UIColor colorWithRed:0.0 green:0.48 blue:1.0 alpha:1.0]];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            break;
            
        case CustomButtonStyleSecondary:
            [self setBackgroundColor:[UIColor colorWithRed:0.95 green:0.95 blue:0.97 alpha:1.0]];
            [self setTitleColor:[UIColor colorWithRed:0.0 green:0.48 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
            break;
            
        case CustomButtonStyleDanger:
            [self setBackgroundColor:[UIColor colorWithRed:1.0 green:0.23 blue:0.19 alpha:1.0]];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            break;
            
        case CustomButtonStyleSuccess:
            [self setBackgroundColor:[UIColor colorWithRed:0.2 green:0.78 blue:0.35 alpha:1.0]];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            break;
    }
    
    // 设置禁用状态
//    [self setBackgroundColor:[self.backgroundColor colorWithAlphaComponent:0.5] forState:UIControlStateDisabled];
}

- (void)setButtonStyle:(CustomButtonStyle)buttonStyle {
    _buttonStyle = buttonStyle;
    [self updateButtonAppearance];
}

- (void)setButtonTitle:(NSString *)title {
    [self setTitle:title forState:UIControlStateNormal];
    self.originalTitle = title;
}

- (void)setButtonIcon:(UIImage *)icon {
    [self setImage:icon forState:UIControlStateNormal];
    self.originalImage = icon;
    
    // 调整图片和标题的间距
    self.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);
}

- (void)startLoading {
    if (self.isLoading) return;
    
    self.isLoading = YES;
    self.enabled = NO;
    
    // 保存原始状态
    self.originalTitle = [self titleForState:UIControlStateNormal];
    self.originalImage = [self imageForState:UIControlStateNormal];
    
    // 隐藏标题和图片
    [self setTitle:@"" forState:UIControlStateNormal];
    [self setImage:nil forState:UIControlStateNormal];
    
    // 显示加载指示器
    [self.loadingIndicator startAnimating];
}

- (void)stopLoading {
    if (!self.isLoading) return;
    
    self.isLoading = NO;
    self.enabled = YES;
    
    // 恢复原始状态
    [self setTitle:self.originalTitle forState:UIControlStateNormal];
    [self setImage:self.originalImage forState:UIControlStateNormal];
    
    // 隐藏加载指示器
    [self.loadingIndicator stopAnimating];
}

- (void)buttonTapped {
    if (self.onTap) {
        self.onTap();
    }
}

#pragma mark - Touch Events

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    
    if (highlighted) {
        self.alpha = 0.7;
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1.0;
        }];
    }
}

@end
